﻿#include "u_quality_alarm_page.h"

static const QString state[] =
{
    "遥控状态", "手控状态", "准备状态", "播出状态", "低功率状态", "联锁报警", "激励报警", "主电源报警",
    "低压报警", "调制报警", "攻放报警", "过流报警", "反射报警", "播出故障", "备用", "备用"
};

QualityAlarmPage::QualityAlarmPage(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    /*this->SetupConnections()*/;
    this->Initialize();
    this->updateTable();
    this->setTitle("状态历史");
    this->resize(1500, 700);
    this->setModal(false);
    connect(btnQuery,SIGNAL(clicked(bool)),this,SLOT(update()));
}

void QualityAlarmPage::update()
{
    this->Initialize();
    this->updateTable();
}

void QualityAlarmPage::InitWidget(){
    btnQuery  = new QPushButton(QChar(0xe639) + QString(" 查询"));

    QList<Equipment> equList = globalData->sqlBase->GetEquipmentList();
    editEquCode = new QComboBox();
    for(int i = 0; i<equList.size(); i++){
        Equipment equ = equList.at(i);
        editEquCode->addItem(equ.devname);
    }

    editStartTime   = new QDateTimeEdit();  editStartTime->setDisplayFormat("yyyy-MM-dd HH:mm:ss"); editStartTime->setCalendarPopup(true);/*弹出日历窗口，当存在有效的日期显示格式时，此属性才有效。*/  editStartTime->setDateTime(QDateTime::currentDateTime().addSecs(-10));//比当前时间早7.2秒
    editEndTime     = new QDateTimeEdit();    editEndTime->setDisplayFormat("yyyy-MM-dd HH:mm:ss"); editEndTime->setCalendarPopup(true);   editEndTime->setDateTime(QDateTime::currentDateTime());

    tableWidget1 =new QTableWidget();
    tableWidget1->setColumnCount(38);
    tableWidget1->setHorizontalHeaderLabels(QStringList()<<"遥控状态"<< "手控状态"<< "准备状态"<< "播出状态"<< "低功率状态"<< "联锁报警"<< "激励报警"<< "主电源报警"<<
                                            "低压报警"<< "调制报警"<< "攻放报警"<< "过流报警"<< "反射报警"<< "播出故障");
    //tableWidget1->horizontalHeader()->setStretchLastSection(true);
    //tableWidget1->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    tableWidget1->setAlternatingRowColors(true);
    tableWidget1->setColumnWidth(1,170);

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addWidget(new QLabel("设备:"));
    btnLayout->addWidget(editEquCode);
    btnLayout->addWidget(new QLabel("开始时间:"));
    btnLayout->addWidget(editStartTime);
    btnLayout->addWidget(new QLabel("结束时间:"));
    btnLayout->addWidget(editEndTime);
    btnLayout->addStretch();
    btnLayout->addWidget(btnQuery);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(btnLayout);

    //-------------------------------------------------02 0F 12 1B 21 2A

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(groupbox11);
    mainLayout->addWidget(tableWidget1);
    this->addLayout(mainLayout);
}

void QualityAlarmPage::Initialize()
{
    QString name=editEquCode->currentText();
    QDateTime tr=editStartTime->dateTime();
    QDateTime et=editEndTime->dateTime();

    StatrList=globalData->sqlBase->GetStateList(name,tr,et);
}

void QualityAlarmPage::updateTable()
{
    int count = StatrList.size();

    tableWidget1->setRowCount(0);
    tableWidget1->setRowCount(count);

    for(int i=0; i<count; i++){
        State info = StatrList.at(i);

        tableWidget1->setItem(i, 0, MyGUI::GetTableWidgetItem(info.devname));
        tableWidget1->setItem(i, 1, MyGUI::GetTableWidgetItem(info.datetime));
        labList.clear();
        int row = 2;
        for(int a=0;a<16;a++)
        {
            QLabel *lab =new QLabel();
            labList.append(lab);

            if(state[a] == "备用"){
                continue;
            }

            QHBoxLayout *layout = new QHBoxLayout();
            layout->setMargin(0);
            layout->addStretch();
            layout->addWidget(lab);
            layout->addStretch();
            QWidget *widget = new QWidget();
            widget->setLayout(layout);

            //tableWidget1->setItem(row<<2<< new QTableWidgetItem(stateText[i]));
            tableWidget1->setCellWidget(i, row, widget);
            row++;
        }
        QList<int> stateList = info.getDataList();
        for(int c=0;c<16;c++)
        {
            labList.at(c)->setStyleSheet(stateList.at(c) == 0 ? RED : GREEN);
        }
    }
}




